
package kr.makjang.lib.net.http;

import java.io.IOException;
import java.util.Vector;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;

/**
 * @date 2011-12-12
 * @author FateInDestiny
 * @package kr.makjang.lib.net.http
 * @content HttpClient Class
 */
public class MakHttpClientPost {

    private String mCookieName = null;

    private String mCookie = null;

    private HttpClient mHttpClient;

    // Constructor
    public MakHttpClientPost() {
        mHttpClient = new DefaultHttpClient();
    }

    public void setCookei(String cookiename, String cookie) {
        mCookieName = cookiename;
        mCookie = cookie;
    }

    /**
     * url의 주소로 strData를 Post방식으로 전달 및 Request를 받음
     * 
     * @param url Target URL
     * @param dataName Data Key Name
     * @param strData Data
     * @return Post 전달 후 Request String
     * @throws ClientProtocolException
     * @throws IOException
     */
    public String httpResponse(String url, String key, String strData)
            throws ClientProtocolException, IOException {
        String result = "";
        String cookie = mCookie;
        String cookieName = mCookieName;

        HttpPost request = new HttpPost(url);
        HttpEntity entity;
        HttpResponse response;

        if (cookieName != null && cookie != null)
            request.setHeader(cookie, cookieName);

        Vector<NameValuePair> nameValue = new Vector<NameValuePair>();

        nameValue.add(new BasicNameValuePair(key, strData));

        // Post로 데이터를 보내는 방식을 UTF-8로 설정
        entity = new UrlEncodedFormEntity(nameValue, HTTP.UTF_8);

        request.setEntity(entity);
        response = mHttpClient.execute(request);
        result = EntityUtils.toString(response.getEntity());

        return result;
    }

    /**
     * url의 주소로 json(JSON 형식 데이터)를 Post 방식으로 전달 및 Request를 받음
     * 
     * @param url Target URL
     * @param dataName Data Key Name
     * @param strData JSON Data
     * @return Post 전달 후 Request String
     * @throws ClientProtocolException
     * @throws IOException
     */
    public String httpResponse(String url, String key, JSONObject json)
            throws ClientProtocolException, IOException {
        String result = "";
        String cookie = mCookie;
        String cookieName = mCookieName;

        HttpPost request = new HttpPost(url);
        HttpEntity entity;
        HttpResponse response;

        if (cookieName != null && cookie != null)
            request.setHeader(cookie, cookieName);

        Vector<NameValuePair> nameValue = new Vector<NameValuePair>();

        nameValue.add(new BasicNameValuePair(key, json.toString()));

        // Post로 데이터를 보내는 방식을 UTF-8로 설정
        entity = new UrlEncodedFormEntity(nameValue, HTTP.UTF_8);

        request.setEntity(entity);
        response = mHttpClient.execute(request);
        result = EntityUtils.toString(response.getEntity());

        return result;
    }
} // end of class
